package it.acweb.pauseless.pauselessapp;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.content.Context;
import android.widget.Toast;

public class xml_utils {

	private static Context con;
	
	public static void setContext(Context context) {
		con = context;
	}
	
	public static void alert (String msg) {
		Toast.makeText(con,
			      msg, Toast.LENGTH_LONG)
			      .show();
	}

	public static Document get_main_document (int id) throws ParserConfigurationException, SAXException, IOException {

        InputStream is = con.getResources().openRawResource(id);
        
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource(is));
        doc.getDocumentElement().normalize();
            
		return doc;
	}
	
	public static Document get_main_document (String filename) throws ParserConfigurationException, SAXException, IOException {

        InputStream is = new FileInputStream(file_utils.getBasePath() + "/" + filename); 
        //con.getResources().getAssets().open(file_utils.getBasePath() + "/" + filename);
        
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource(is));
        doc.getDocumentElement().normalize();
            
		return doc;
	}
	
	public static Node get_cd (Document doc, String name) throws Exception {
		NodeList nodeList = doc.getElementsByTagName("cd");
		Node node;
		
        for (int i = 0; i < nodeList.getLength(); i++) {
        	Element el = (Element)nodeList.item(i);
        	if (el.getAttribute("name").equals(name)) {
        		node = nodeList.item(i);
        		return node;
        	}
        }
		
        throw new Exception("CD "+name+" non trovato");

	}
	
	public static Node get_mix (Document doc, String name) throws Exception {
		NodeList nodeList = doc.getElementsByTagName("mix");
		Node node;
		
        for (int i = 0; i < nodeList.getLength(); i++) {
        	Element el = (Element)nodeList.item(i);
        	String nm = el.getAttribute("name") + " - " + el.getAttribute("bpm");
        	if (nm.equals(name)) {
        		node = nodeList.item(i);
        		return node;
        	}
        }
		
        throw new Exception("Mix "+name+" non trovato");

	}
	
	private static String[] get_pauses (Node song) throws Exception {
		NodeList nodeList = ((Element)song).getElementsByTagName("pause");
		String[] pauses = new String[nodeList.getLength()];

        for (int i = 0; i < nodeList.getLength(); i++) {
        	Element el = (Element)nodeList.item(i);
        	pauses[i] = el.getTextContent();
        }
		
        return pauses;

	}
	
	public static String[] get_songs_titles (Node node) throws Exception {
		
        NodeList songs;
        Element el;

		if(node instanceof Element) {
		    Element nodeElement = (Element)node;
		    songs = nodeElement.getElementsByTagName("song");
    		String[] values = new String[songs.getLength()];
            for (int y = 0; y < songs.getLength(); y++) {
            	el = (Element)songs.item(y);
                values[y] = el.getAttribute("title");
            }
            
            return values;

		}
		
        throw new Exception("Nessuna canzone");
	}

	public static Song[] get_songs (Node node) throws Exception {
		
        NodeList songs;
        Element el;
        Song so = null;

		if(node instanceof Element) {
		    Element nodeElement = (Element)node;
		    String path = nodeElement.getAttribute("name");
		    songs = nodeElement.getElementsByTagName("song");
    		Song[] values= new Song[songs.getLength()];
            for (int y = 0; y < songs.getLength(); y++) {
            	el = (Element)songs.item(y);
            	so = new Song(el.getAttribute("track"), el.getAttribute("artist"), el.getAttribute("title"), el.getAttribute("bpm"), path, get_pauses(songs.item(y)));
                values[y] = so;
            }

            return values;

		}
		
        throw new Exception("Nessuna canzone");
	}
	
	public static Song getSongByPath (String path) {
		try {
			String[] temp;
			temp = path.split("/");
	        Document doc = get_main_document(R.raw.cd_list);
			Node cd = get_cd(doc, temp[0]);
			Song[] temp2 = get_songs(cd);
			int temp3 = Integer.parseInt(temp[1]);
			return(temp2[temp3 - 1]);
		} catch (Exception e) {
			return new Song("?", path, "not found", "?", path, new String[0]);
		}
	}
	
	public static Song[] get_mix_songs (Node node) throws Exception {
		
        NodeList songs;
        Element el;
        Song so = null;
		if(node instanceof Element) {
		    Element nodeElement = (Element)node;
		    songs = nodeElement.getElementsByTagName("song");
    		Song[] values= new Song[songs.getLength()];
            for (int y = 0; y < songs.getLength(); y++) {
            	el = (Element)songs.item(y);
            	so = getSongByPath(el.getAttribute("path"));
                values[y] = so;
            }

            return values;

		}
		
        throw new Exception("Nessuna canzone");
	}

//	public static Song[] search_by_bpm (String bpm) {
//		
//	}

}
